package web.servlet;

import java.security.Security;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.net.ssl.internal.ssl.Provider;

public class SendMail {
	private String from = "337806034@qq.com";
	private String user = "337806034";
	String password = "你猜";

	public SendMail() {

	}

	public void sendMail(String to, String text, String titile) {
		Properties props = new Properties();
		props.setProperty("mail.smtp.host", "smtp.qq.com");
		props.put("mail.smtp.host", "smtp.qq.com");
		props.put("mail.smtp.auth", "true");// ��������֤
		Session session = Session.getDefaultInstance(props);
		session.setDebug(true);
		// ��֤
		Security.addProvider(new Provider());
		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.port", "465");
		props.setProperty("mail.smtp.socketFactory.port", "465");
		//
		MimeMessage message = new MimeMessage(session);

		try {
			message.setFrom(new InternetAddress(from));// ���÷�����
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			message.setSubject(titile);
			Multipart multipient = new MimeMultipart();// ���ø���
			BodyPart contentPart = new MimeBodyPart();
			contentPart.setContent(text, "text/html;charset=utf-8");
			multipient.addBodyPart(contentPart);
			message.setContent(multipient);
			message.saveChanges();
			Transport transport = session.getTransport("smtp");
			transport.connect("smtp.qq.com", user, password);
			System.out.println("7");
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String sendRegistMail(String to, String userName) {
		String registerId = "" + Math.random() * Math.random();
		String url = "";
		String text = "";
		String titile = "";
		sendMail(to, text, titile);
		return registerId;
	}
}
